home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C21 / ForEach.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  808 b   |  35 lines

  1. //: C21:ForEach.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Use of STL for_each() algorithm
  7. #include "Counted.h"
  8. #include <iostream>
  9. #include <vector>
  10. #include <algorithm>
  11. using namespace std;
  12.  
  13. // Simple function:
  14. void destroy(Counted* fp) { delete fp; }
  15.  
  16. // Function object:
  17. template<class T>
  18. class DeleteT {
  19. public:
  20.   void operator()(T* x) { delete x; }
  21. };
  22.  
  23. // Template function:
  24. template <class T>
  25. void wipe(T* x) { delete x; }
  26.  
  27. int main() {
  28.   CountedVector A("one");
  29.   for_each(A.begin(), A.end(), destroy);
  30.   CountedVector B("two");
  31.   for_each(B.begin(),B.end(),DeleteT<Counted>());
  32.   CountedVector C("three");
  33.   for_each(C.begin(), C.end(), wipe<Counted>);
  34. } ///:~
  35.